let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    week_list: [
      { weekday_number: 1, name: 'دۈشەنبە', isActive: false },
      { weekday_number: 2, name: 'سەيشەنبە', isActive: false },
      { weekday_number: 3, name: 'چارشەنبە', isActive: false },
      { weekday_number: 4, name: 'پەيشەنبە', isActive: false },
      { weekday_number: 5, name: 'جۈمە', isActive: false },
      { weekday_number: 6, name: 'شەنبە', isActive: false },
      { weekday_number: 7, name: 'يەكشەنبە', isActive: false },
    ],
    signCount: 0,
    dialog: false,
    oneVIPdialog: false,
  },

  // 检查用户登录
  isLogin() {
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      this.setData({ isLoginMask: true })
    }
  },

  // 隐藏登录提示组件
  changeLoginMask(e) {
    this.createSign();
    this.setData({ isLoginMask: e.detail.isLoginMask})
  },

  // 领包
  onLingBao() {
    if(this.data.signCount == 7){
      this.setData({ oneVIPdialog: true })
      let userInfo = wx.getStorageSync('userInfo');
      let user_id = userInfo.user.id;
      
      let data = { user_id: user_id, vip_days: 1 }
      let that = this;
      wx.request({
        url: `${app.globalData.API_URL}/oneVipUserAuth`,
        method: 'POST',
        data: data,
        header: app.header(),
        success(res) {
          if (res.statusCode == 200) {
            app.globalData.isVIPAuthority = true;
          }
        },
      });


    } else if(this.data.signCount < 7) {
      this.setData({ dialog: true })
    }
  },

  // 关闭领包对话框
  onClose() {
    this.setData({ dialog: false, oneVIPdialog: false })
  },

  // 签到
  createSign() {
    let userInfo = wx.getStorageSync('userInfo');
    let user_id;
    if(!userInfo) return;
    user_id = userInfo.user.id;
    
    let data = { user_id: user_id }
    let that = this;
    wx.request({
      url: `${app.globalData.API_URL}/sign`,
      method: 'POST',
      data: data,
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.statusCode) {
          let week_list = that.data.week_list;
          for(let item of week_list) {
            for (let e of res.data.weekSigns) {
              item.weekday_number == e.weekday_number ? item.isActive = true : ''
            }
          }
          that.setData({ week_list, signCount: res.data.weekSigns.length });
        }
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.isLogin();
    this.createSign();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: 'قىيات ئاۋازلىق ئەسەر',
      path: '/pages/signIn/signIn',
      imageUrl: 'https://mmecoa.qpic.cn/sz_mmecoa_png/iauAIibMdU78thJHXKBcIfYLNA6zH3pbFllMLlSibhVriaGwtS0bM1tLLyz6OSlQ74icF6c4Yic4icLokYGvb2jXnY8bA/640?wx_fmt=png&amp;from=appmsg'
    };
  }
})